import Vue from "vue";
import VueRouter from "vue-router";
import store from "../store/index";
// Vue.use(VueRouter)
Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    // redirect重定向
    redirect: "IndividualShare",
    component: () => import("../views/HomePage.vue"),
    meta: {
      title: "首页",
    },
  },
  {
    path: "/HomePage",
    // redirect重定向
    name: "HomePage",
    component: () => import("../views/HomePage.vue"),
    meta: {
      title: "首页",
    },
  },
  {
    path: "/IndividualShare",
    name: "IndividualShare",
    component: () => import("../views/IndividualShare.vue"),
    meta: {
      title: "个股",
    },
  },
  {
    path: "/MarketPlace",
    name: "MarketPlace",
    component: () => import("../views/MarketPlace.vue"),
    meta: {
      title: "市场",
    },
  },
  {
    path: "/OptionalStock",
    name: "OptionalStock",
    component: () => import("../views/OptionalStock.vue"),
    meta: {
      title: "自选",
    },
  },
  {
    path: "/HoldAPosition",
    name: "HoldAPosition",
    component: () => import("../views/HoldAPosition.vue"),
    meta: {
      title: "持仓",
    },
  },
  {
    path: "/team",
    name: "team",
    component: () => import("../views/team.vue"),
    meta: {
      title: "团队",
    },
  },
  {
    path: "/PersonalCenter",
    name: "PersonalCenter",
    component: () => import("../views/PersonalCenter.vue"),
    meta: {
      title: "我的",
    },
  },
  {
    path: "/case",
    name: "case",
    component: () => import("../views/case.vue"),
    meta: {
      title: "案例",
    },
  },
  {
    path: "/HelloWorld",
    name: "HelloWorld",
    component: () => import("../components/HelloWorld.vue"),
  },
  {
    path: "/simulatedPosition",
    name: "simulatedPosition",
    component: () =>
      import("../components/HoldAPosition/simulatedPosition.vue"),
  },
];

const router = new VueRouter({
  // 配置路由和组件之间的应用关系
  routes,
  mode: "hash",
  linkActiveClass: "active",
});
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
router.beforeEach((to, from, next) => {
  let token = store.state.login.token || localStorage.getItem("token");
  console.log();
  if (to.path == '/case') {
    next()
  } else {
    if (token) {
      next()
    } else {
      next({ path: '/case' })
    }
  }


})
export default router;
